/**
 * 详情对话框
 */
var TMonitoringinfoInfoDlg = {
    data: {
        companyId: "",
        drain: "",
        detectTime: "",
        monitoringFactor: "",
        monitorConcentration: "",
        emissionRate: "",
        pollutantEmissions: "",
        monitoringReport: "",
        emissionCalculationProcess: "",
        monitoringPlan: ""

    }
};

layui.use(['form', 'admin','laydate','upload',  'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var upload = layui.upload;

    //让当前iframe弹层高度适应
    //admin.iframeAuto();

    // 渲染时间选择框
    laydate.render({
        elem: '#detectTime'
    });

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/tMonitoringinfo/detail?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();
    form.val('tMonitoringinfoForm', result.data);
    if (result.data.monitoringReport!=null&&result.data.monitoringReport!=""){
          var srr = result.data.monitoringReport.split(",");
          for (var i in srr ){
              $("#shabi").attr("src",srr[i]);
          }
    }
    if (result.data.emissionCalculationProcess!=null&&result.data.emissionCalculationProcess!=""){
        $("#processImg").attr("src",result.data.emissionCalculationProcess);
    }
    if (result.data.monitoringPlan!=null&&result.data.monitoringPlan!=""){
        $("#planImg").attr("src",result.data.monitoringPlan);
    }


    var str1 = result.data.monitoringReport.split(",");
    //  多图回显
        if(result.data.monitoringReport != ''){
            for (var i =0;i<str1.length;i++){
                $('#upload_image_list').append('<img id= "shabi" style="height: 66px;margin-left: 7px" src="'+ str1[i] +'" title="单击删除" οnclick="delMultipleImgs(this)" class="layui-upload-img">');
            }
        }



    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/tMonitoringinfo/editItem", function (data) {
            if(data.code == 200) {
                Feng.success("更新成功！");
            }else{
                Feng.error("更新失败！" + data.message)
            }

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            //关掉对话框
            admin.closeThisDialog();

        }, function (data) {
            Feng.error("更新失败！" + data.message)
        });

        // data.field.monitoringReport =  $('#monitoringReport').val();
        data.field.emissionCalculationProcess =  $('#emissionCalculationProcess').val();
        data.field.monitoringPlan =  $('#monitoringPlan').val();
        data.field.monitoringReport = $('#monitoringReport').val();
        ajax.set(data.field);
        ajax.start();

        return false;
    });






    /**
     * 上传监测报告
     */
    var uploadInst = upload.render({
        elem: '#reportImageBtn'
        ,url: Feng.ctxPath+'/ali/uploadFile'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                loading = layer.load(0, {
                    shade: false,
                    time: 10*1000
                })
            });
        }
        ,done: function(res){
            console.log(res);
            layer.close(loading);
            $('#reporturlImg').attr('src', res.data);
            $('#monitoringReport').attr("value", res.data);
            //上传成功
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#reporturlImg');
            layer.close(loading);
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a '+'class="layui-btn layui-btn-xs demo-reload">重试</ a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });


    /**
     * 上传排放量计算过程
     */
    var uploadInst = upload.render({
        elem: '#processImageBtn'
        ,url: Feng.ctxPath+'/ali/uploadFile'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                loading = layer.load(0, {
                    shade: false,
                    time: 10*1000
                })
            });
        }
        ,done: function(res){
            console.log(res);
            layer.close(loading);
            $('#processImg').attr('src', res.data);
            $('#emissionCalculationProcess').attr("value", res.data);
            //上传成功
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#processImg');
            layer.close(loading);
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a '+'class="layui-btn layui-btn-xs demo-reload">重试</ a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });



    /**
     * 上传监测计划
     */
    var uploadInst = upload.render({
        elem: '#planImageBtn'
        ,url: Feng.ctxPath+'/ali/uploadFile'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                loading = layer.load(0, {
                    shade: false,
                    time: 10*1000
                })
            });
        }
        ,done: function(res){
            console.log(res);
            layer.close(loading);
            $('#planImg').attr('src', res.data);
            $('#monitoringPlan').attr("value", res.data);
            //上传成功
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#planImg');
            layer.close(loading);
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a '+'class="layui-btn layui-btn-xs demo-reload">重试</ a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });


    //监听有无组织

    form.on('select(isOrganization)', function (data) {
        console.log(data.elem); //得到select原始DOM对象
        console.log(data.value); //得到被选中的值
        console.log(data.othis); //得到美化后的DOM对象
        var  sb = document.getElementById("isOpen");

        console.log(sb); //得到select原始DOM对象
        if (data.value == 1) {
            sb.style.display = "none"

        }else {
            sb.style.display = "block"
        }
    });


    /*多图上传*/
    var uploadInst  = upload.render({
        elem: '#btn_multiple_upload_img'
        ,url:  Feng.ctxPath+'/ali/uploadFile' //改成您自己的上传接口
        ,multiple: true
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#upload_image_list').append('<img id= "shabi" style="height: 66px;margin-left: 7px" src="'+ result +'" title="单击删除" οnclick="delMultipleImgs(this)" class="layui-upload-img">');
            });
        }
        ,done: function(res){
            //上传完毕
            if (res.code== 0) {

                var last_url = $(".upload_image_url").val();
                var upload_image_url = "";
                if(last_url){
                    upload_image_url = last_url+","+res.data;
                    $("#monitoringReport").val(upload_image_url);

                }else {
                    upload_image_url = res.data;
                    $("#monitoringReport").val( upload_image_url);
                }
                $(".upload_image_url").val(upload_image_url);



            }
        }
    });

    /**
     * 多图清除按钮点击事件
     */
    $("#btn_image_clear").click(function () {
        $('#upload_image_list').html("");
        $(".upload_image_url").val('');
    });

    function delMultipleImgs(this_img) {
        //获取下标
        var subscript = $("#upload_image_list img").index(this_img);
        multiple_images = $('.upload_image_url').val().split(",");
        //删除图片
        this_img.remove();
        //删除数组
        multiple_images.splice(subscript, 1);
        $('.upload_image_url').val(multiple_images);
    };


    //删除单个图片
    $('#upload_image_list').on('click', $('shabi'), function(data){
        var subscript = $("#upload_image_list img").index(data.target);
        multiple_images = $('.upload_image_url').val().split(",");
        //删除图片
        data.target.remove();
        //删除数组
        multiple_images.splice(subscript, 1);
        $('.upload_image_url').val(multiple_images);

    })



});